﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Windows.ApplicationModel.Resources.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using System.Collections.Specialized;

// The data model defined by this file serves as a representative example of a strongly-typed
// model that supports notification when members are added, removed, or modified.  The property
// names chosen coincide with data bindings in the standard item templates.
//
// Applications may use this model as a starting point and build on it, or discard it entirely and
// replace it with something appropriate to their needs.

namespace Education.Data
{
    /// <summary>
    /// Base class for <see cref="Spoke1DataItem"/> and <see cref="Spoke1DataGroup"/> that
    /// defines properties common to both.
    /// </summary>
    [Windows.Foundation.Metadata.WebHostHidden]
    public abstract class Spoke1DataCommon : Education.Common.BindableBase
    {
        private static Uri _baseUri = new Uri("ms-appx:///");

        public Spoke1DataCommon(String uniqueId, String title, String subtitle, String imagePath, String description)
        {
            this._uniqueId = uniqueId;
            this._title = title;
            this._subtitle = subtitle;
            this._description = description;
            this._imagePath = imagePath;
        }

        private string _uniqueId = string.Empty;
        public string UniqueId
        {
            get { return this._uniqueId; }
            set { this.SetProperty(ref this._uniqueId, value); }
        }

        private string _title = string.Empty;
        public string Title
        {
            get { return this._title; }
            set { this.SetProperty(ref this._title, value); }
        }

        private string _subtitle = string.Empty;
        public string Subtitle
        {
            get { return this._subtitle; }
            set { this.SetProperty(ref this._subtitle, value); }
        }

        private string _description = string.Empty;
        public string Description
        {
            get { return this._description; }
            set { this.SetProperty(ref this._description, value); }
        }

        private ImageSource _image = null;
        private String _imagePath = null;
        public ImageSource Image
        {
            get
            {
                if (this._image == null && this._imagePath != null)
                {
                    this._image = new BitmapImage(new Uri(Spoke1DataCommon._baseUri, this._imagePath));
                }
                return this._image;
            }

            set
            {
                this._imagePath = null;
                this.SetProperty(ref this._image, value);
            }
        }

        public void SetImage(String path)
        {
            this._image = null;
            this._imagePath = path;
            this.OnPropertyChanged("Image");
        }

        public override string ToString()
        {
            return this.Title;
        }
    }

    /// <summary>
    /// Generic item data model.
    /// </summary>
    public class Spoke1DataItem : Spoke1DataCommon
    {
        public Spoke1DataItem(String uniqueId, String title, String subtitle, String imagePath, String description, String content,int ColSpan, int RowSpan, Spoke1DataGroup group)
            : base(uniqueId, title, subtitle, imagePath, description)
        {
            this._content = content;
            this._ColSpan = ColSpan;
            this._RowSpan = RowSpan;
            this._group = group;
        }

        private string _content = string.Empty;
        public string Content
        {
            get { return this._content; }
            set { this.SetProperty(ref this._content, value); }
        }

        private string _TileHeight = string.Empty;
        public string TileHeight
        {
            get { return this._TileHeight; }
            set { this.SetProperty(ref this._TileHeight, value); }
        }

        private string _TileClr = string.Empty;
        public string TileClr
        {
            get { return this._TileClr; }
            set { this.SetProperty(ref this._TileClr, value); }
        }                

        private int _ColSpan = 1;
        public int ColSpan
        {
            get { return this._ColSpan; }
            set { this.SetProperty(ref this._ColSpan, value); }
        }

        private int _RowSpan = 1;
        public int RowSpan
        {
            get { return this._RowSpan; }
            set { this.SetProperty(ref this._RowSpan, value); }
        }

        private Spoke1DataGroup _group;
        public Spoke1DataGroup Group
        {
            get { return this._group; }
            set { this.SetProperty(ref this._group, value); }
        }
    }

    /// <summary>
    /// Generic group data model.
    /// </summary>
    public class Spoke1DataGroup : Spoke1DataCommon
    {
        public Spoke1DataGroup(String uniqueId, String title, String subtitle, String imagePath, String description)
            : base(uniqueId, title, subtitle, imagePath, description)
        {
            Items.CollectionChanged += ItemsCollectionChanged;
        }

        private void ItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            // Provides a subset of the full items collection to bind to from a GroupedItemsPage
            // for two reasons: GridView will not virtualize large items collections, and it
            // improves the user experience when browsing through groups with large numbers of
            // items.
            //
            // A maximum of 12 items are displayed because it results in filled grid columns
            // whether there are 1, 2, 3, 4, or 6 rows displayed

            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    if (e.NewStartingIndex < 12)
                    {
                        TopItems.Insert(e.NewStartingIndex,Items[e.NewStartingIndex]);
                        if (TopItems.Count > 12)
                        {
                            TopItems.RemoveAt(12);
                        }
                    }
                    break;
                case NotifyCollectionChangedAction.Move:
                    if (e.OldStartingIndex < 12 && e.NewStartingIndex < 12)
                    {
                        TopItems.Move(e.OldStartingIndex, e.NewStartingIndex);
                    }
                    else if (e.OldStartingIndex < 12)
                    {
                        TopItems.RemoveAt(e.OldStartingIndex);
                        TopItems.Add(Items[11]);
                    }
                    else if (e.NewStartingIndex < 12)
                    {
                        TopItems.Insert(e.NewStartingIndex, Items[e.NewStartingIndex]);
                        TopItems.RemoveAt(12);
                    }
                    break;
                case NotifyCollectionChangedAction.Remove:
                    if (e.OldStartingIndex < 12)
                    {
                        TopItems.RemoveAt(e.OldStartingIndex);
                        if (Items.Count >= 12)
                        {
                            TopItems.Add(Items[11]);
                        }
                    }
                    break;
                case NotifyCollectionChangedAction.Replace:
                    if (e.OldStartingIndex < 12)
                    {
                        TopItems[e.OldStartingIndex] = Items[e.OldStartingIndex];
                    }
                    break;
                case NotifyCollectionChangedAction.Reset:
                    TopItems.Clear();
                    while (TopItems.Count < Items.Count && TopItems.Count < 12)
                    {
                        TopItems.Add(Items[TopItems.Count]);
                    }
                    break;
            }
        }

        private ObservableCollection<Spoke1DataItem> _items = new ObservableCollection<Spoke1DataItem>();
        public ObservableCollection<Spoke1DataItem> Items
        {
            get { return this._items; }
        }

        private ObservableCollection<Spoke1DataItem> _topItem = new ObservableCollection<Spoke1DataItem>();
        public ObservableCollection<Spoke1DataItem> TopItems
        {
            get {return this._topItem; }
        }
    }

    /// <summary>
    /// Creates a collection of groups and items with hard-coded content.
    /// 
    /// Spoke1DataSource initializes with placeholder data rather than live production
    /// data so that Spoke1 data is provided at both design-time and run-time.
    /// </summary>
    public sealed class Spoke1DataSource
    {
        private static Spoke1DataSource _Spoke1DataSource = new Spoke1DataSource();

        private ObservableCollection<Spoke1DataGroup> _allGroups = new ObservableCollection<Spoke1DataGroup>();
        public ObservableCollection<Spoke1DataGroup> AllGroups
        {
            get { return this._allGroups; }
        }

        public static IEnumerable<Spoke1DataGroup> GetGroups(string uniqueId)
        {
            if (!uniqueId.Equals("AllGroups")) throw new ArgumentException("Only 'AllGroups' is supported as a collection of groups");
            
            return _Spoke1DataSource.AllGroups;
        }

        public static Spoke1DataGroup GetGroup(string uniqueId)
        {
            // Simple linear search is acceptable for small data sets
            var matches = _Spoke1DataSource.AllGroups.Where((group) => group.UniqueId.Equals(uniqueId));
            if (matches.Count() == 1) return matches.First();
            return null;
        }

        public static Spoke1DataItem GetItem(string uniqueId)
        {
            // Simple linear search is acceptable for small data sets
            var matches = _Spoke1DataSource.AllGroups.SelectMany(group => group.Items).Where((item) => item.UniqueId.Equals(uniqueId));
            if (matches.Count() == 1) return matches.First();
            return null;
        }

        public Spoke1DataSource()
        {
            String ITEM_CONTENT = String.Format("Item Content: {0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}",
                        "Curabitur class aliquam vestibulum nam curae maecenas sed integer cras phasellus suspendisse quisque donec dis praesent accumsan bibendum pellentesque condimentum adipiscing etiam consequat vivamus dictumst aliquam duis convallis scelerisque est parturient ullamcorper aliquet fusce suspendisse nunc hac eleifend amet blandit facilisi condimentum commodo scelerisque faucibus aenean ullamcorper ante mauris dignissim consectetuer nullam lorem vestibulum habitant conubia elementum pellentesque morbi facilisis arcu sollicitudin diam cubilia aptent vestibulum auctor eget dapibus pellentesque inceptos leo egestas interdum nulla consectetuer suspendisse adipiscing pellentesque proin lobortis sollicitudin augue elit mus congue fermentum parturient fringilla euismod feugiat");

            var group1 = new Spoke1DataGroup("Group-1",
                    "overview",
                    "Group Subtitle: 1",
                    "Assets/DarkGray.png",
                    "Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor scelerisque lorem in vehicula. Aliquam tincidunt, lacus ut sagittis tristique, turpis massa volutpat augue, eu rutrum ligula ante a ante");
            group1.Items.Add(new Spoke1DataItem("Overview-Group-1-Item-1",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec elit erat. Duis scelerisque turpis eget tortor rhoncus dictum. Vestibulum interdum iaculis ligula, ultrices egestas felis congue ac. Etiam rhoncus molestie vestibulum. Praesent rutrum, purus id semper vulputate, enim sapien egestas turpis, sed pulvinar ipsum lacus sed odio. Ut sed diam erat, vestibulum pellentesque neque. Morbi feugiat consequat convallis.",
                    "Fusce in massa et risus iaculis semper. Proin ante dui, cursus eget tempor et, consectetur ut odio. Fusce mollis elementum hendrerit. Nullam elementum odio ut est auctor pulvinar. In ac mauris ac nunc scelerisque lacinia id id magna. Nulla id velit odio, sit amet rutrum felis. Pellentesque ultricies lacinia commodo. In condimentum bibendum tempor.",
                    "Assets/LightGray.png",
                    "Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Maecenas dignissim, libero a ullamcorper scelerisque, orci felis sodales libero, ac elementum mauris sapien sed mi. Fusce eget mi dui. Duis malesuada blandit eros, eget lacinia orci placerat eget. Vivamus velit lectus, auctor id tristique at, fringilla in ante. Pellentesque aliquet augue nec orci facilisis consequat. Phasellus facilisis sapien sagittis arcu porttitor scelerisque aliquet dui sollicitudin. Etiam quis mauris lectus. Quisque pretium, diam non sagittis ultricies, nulla sapien porta dui, a ultricies sem orci in elit.",
                    ITEM_CONTENT, 44, 50,
                    group1));           
            this.AllGroups.Add(group1);

            var group2 = new Spoke1DataGroup("Group-2",
                    "admission requirements",
                    "Group Subtitle: 2",
                    "Assets/LightGray.png",
                    "Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor scelerisque lorem in vehicula. Aliquam tincidunt, lacus ut sagittis tristique, turpis massa volutpat augue, eu rutrum ligula ante a ante");
            group2.Items.Add(new Spoke1DataItem("Admission-Group-2-Item-1",
                    "high school transcripts",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec elit erat. Duis scelerisque turpis eget tortor rhoncus dictum. Vestibulum interdum iaculis ligula.Etiam rhoncus molestie vestibulum. Praesent rutrum, purus id semper vulputate, enim sapien egestas.",
                    "Assets/DarkGray.png",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec elit erat. Duis scelerisque turpis eget tortor rhoncus dictum. Vestibulum interdum iaculis ligula, ultrices egestas felis congue ac. Etiam rhoncus molestie vestibulum. Praesent rutrum, purus id semper vulputate, enim sapien egestas turpis, sed pulvinar ipsum lacus sed odio. Ut sed diam erat, vestibulum pellentesque neque. Morbi feugiat consequat convallis. Fusce in massa et risus iaculis semper. Proin ante dui, cursus eget tempor et, consectetur ut odio. Fusce mollis elementum hendrerit. Nullam elementum odio ut est auctor pulvinar. In ac mauris ac nunc scelerisque lacinia id id magna. Nulla id velit odio, sit amet rutrum felis. Pellentesque ultricies lacinia commodo. In condimentum bibendum tempor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Maecenas dignissim, libero a ullamcorper scelerisque, orci felis sodales libero, ac elementum mauris sapien sed mi. Fusce eget mi dui. Duis malesuada blandit eros, eget lacinia orci placerat eget. Vivamus velit lectus, auctor id tristique at, fringilla in ante. Pellentesque aliquet augue nec orci facilisis consequat. Phasellus facilisis sapien sagittis arcu porttitor scelerisque aliquet dui sollicitudin. Etiam quis mauris lectus. Quisque pretium, diam non sagittis ultricies, nulla sapien porta dui, a ultricies sem orci in elit.",
                    ITEM_CONTENT, 54, 12,
                    group2) { TileHeight = "110", TileClr = "#FFff5db1" });
            group2.Items.Add(new Spoke1DataItem("Admission-Group-2-Item-2",
                    "test scores (toefl/sat/ielts/act)",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec elit erat. Duis scelerisque turpis eget tortor rhoncus dictum. Etiam rhoncus molestie vestibulum. Praesent rutrum, purus id semper vulputate, enim sapien egestas turpis,",
                    "Assets/MediumGray.png",
                    "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                    ITEM_CONTENT, 54, 12,
                    group2) { TileHeight = "110", TileClr = "#FFff5d5d" });
            group2.Items.Add(new Spoke1DataItem("Admission-Group-2-Item-3",
                    "letter of recommen-dation",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec elit erat. Duis scelerisque turpis eget tortor rhoncus dictum. Etiam rhoncus molestie vestibulum. Praesent rutrum, purus id semper vulputate, enim sapien egestas turpis,",
                    "Assets/LightGray.png",
                    "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                    ITEM_CONTENT, 54, 13,
                    group2) { TileHeight = "120", TileClr = "#FF5064dd" });
            group2.Items.Add(new Spoke1DataItem("Admission-Group-2-Item-4",
                   "essay",
                   "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec elit erat. Duis scelerisque turpis eget tortor rhoncus dictum. Etiam rhoncus molestie vestibulum. Praesent rutrum, purus id semper vulputate, enim sapien egestas turpis,",
                   "Assets/LightGray.png",
                   "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                   ITEM_CONTENT, 54, 13,
                   group2) { TileHeight = "120", TileClr = "#FF89d83b" });
            this.AllGroups.Add(group2);

            var group3 = new Spoke1DataGroup("Group-3",
                    "curriculum",
                    "Group Subtitle: 3",
                    "Assets/MediumGray.png",
                    "Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor scelerisque lorem in vehicula. Aliquam tincidunt, lacus ut sagittis tristique, turpis massa volutpat augue, eu rutrum ligula ante a ante");
            group3.Items.Add(new Spoke1DataItem("Curriculam-Group-3-Item-1",
                    "elements of visual thinking",
                    "ff 100",
                    "Assets/MediumGray.png",
                    "3 credits",
                    ITEM_CONTENT, 16, 17,
                    group3) { TileHeight = "160", TileClr = "#FFd94f97" });
            group3.Items.Add(new Spoke1DataItem("Curriculam-Group-3-Item-2",
                    "sculptural forms",
                    "ff 101",
                    "Assets/LightGray.png",
                    "3 credits",
                    ITEM_CONTENT, 16, 16,
                    group3) { TileHeight = "150", TileClr = "#FFd94f97" });
            group3.Items.Add(new Spoke1DataItem("Curriculam-Group-3-Item-3",
                    "painting",
                    "ff 150",
                    "Assets/DarkGray.png",
                    "3 credits",
                    ITEM_CONTENT, 16, 17,
                    group3) { TileHeight = "160", TileClr = "#FFd94f97" });
            group3.Items.Add(new Spoke1DataItem("Curriculam-Group-3-Item-4",
                    "drawing",
                    "ff 198",
                    "Assets/LightGray.png",
                    "3 credits",
                    ITEM_CONTENT, 16, 17,
                    group3) { TileHeight = "160", TileClr = "#FFd94f97" });
            group3.Items.Add(new Spoke1DataItem("Curriculam-Group-3-Item-5",
                    "electronic media and culture",
                    "ff 210",
                    "Assets/MediumGray.png",
                    "3 credits",
                    ITEM_CONTENT, 16, 16,
                    group3) { TileHeight = "150", TileClr = "#FFd94f97" });
            group3.Items.Add(new Spoke1DataItem("Curriculam-Group-3-Item-6",
                    "foundation studio elective",
                    "-",
                    "Assets/DarkGray.png",
                    "3 credits",
                    ITEM_CONTENT, 16, 17,
                    group3) { TileHeight = "160", TileClr = "#FFd94f97" });
            group3.Items.Add(new Spoke1DataItem("Curriculam-Group-3-Item-7",
                    "critical inquiry",
                    "ff 100",
                    "Assets/MediumGray.png",
                    "3 credits",
                    ITEM_CONTENT, 16, 17,
                    group3) { TileHeight = "160", TileClr = "#FFff7f5d" });
            group3.Items.Add(new Spoke1DataItem("Curriculam-Group-3-Item-8",
                    "intellectual history electives",
                    "ff 101",
                    "Assets/MediumGray.png",
                    "3 credits",
                    ITEM_CONTENT, 16, 16,
                    group3) { TileHeight = "150", TileClr = "#FFff7f5d" });
            group3.Items.Add(new Spoke1DataItem("Curriculam-Group-3-Item-9",
                   "science/math elective",
                   "ff 150",
                   "Assets/DarkGray.png",
                   "3 credits",
                   ITEM_CONTENT, 16, 17,
                   group3) { TileHeight = "160", TileClr = "#FFff7f5d" });
            group3.Items.Add(new Spoke1DataItem("Curriculam-Group-3-Item-10",
                    "art matters",
                    "ff 198",
                    "Assets/MediumGray.png",
                    "3 credits",
                    ITEM_CONTENT, 16, 17,
                    group3) { TileHeight = "160", TileClr = "#FFff7f5d" });
            group3.Items.Add(new Spoke1DataItem("Curriculam-Group-3-Item-11",
                   "modernism and after",
                   "ff 210",
                   "Assets/MediumGray.png",
                   "3 credits",
                   ITEM_CONTENT, 16, 16,
                   group3) { TileHeight = "150", TileClr = "#FFff7f5d" });
            group3.Items.Add(new Spoke1DataItem("Curriculam-Group-3-Item-12",
                   "art history electives",
                   "-",
                   "Assets/MediumGray.png",
                   "3 credits",
                   ITEM_CONTENT, 16, 17,
                   group3) { TileHeight = "160", TileClr = "#FFff7f5d" });
            this.AllGroups.Add(group3);

            var group4 = new Spoke1DataGroup("Group-4",
                    "faculty",
                    "Group Subtitle: 4",
                    "Assets/LightGray.png",
                    "Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor scelerisque lorem in vehicula. Aliquam tincidunt, lacus ut sagittis tristique, turpis massa volutpat augue, eu rutrum ligula ante a ante");
            group4.Items.Add(new Spoke1DataItem("Normal-Group-4-Item-1",
                       "tanay kumar",
                    "professor (art history)",
                     "Assets/HubPage/Img04.jpg",
                    "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                    ITEM_CONTENT, 25, 25,
                    group4) { TileClr = "#D8000000" });
            group4.Items.Add(new Spoke1DataItem("Normal-Group-4-Item-2",
                    "priyanka agarwal",
                    "professor (business ethics)",
                    "Assets/HubPage/Img05.jpg",
                    "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                    ITEM_CONTENT, 25, 25,
                    group4) { TileClr = "#D8000000" });
            group4.Items.Add(new Spoke1DataItem("Normal-Group-4-Item-3",
                    "hemant suthar",
                    "professor (chemistry)",
                    "Assets/HubPage/Img06.jpg",
                    "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                    ITEM_CONTENT, 25, 25,
                    group4) { TileClr = "#D8000000" });
            group4.Items.Add(new Spoke1DataItem("Normal-Group-4-Item-4",
                    "geeta suthar",
                  "professor (economics)",
                     "Assets/HubPage/Img07.jpg",
                    "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                    ITEM_CONTENT, 25, 25,
                    group4) { TileClr = "#D8000000" });    
            this.AllGroups.Add(group4);

           
        }
    }
}
